import urllib3
from bs4 import BeautifulSoup

# 第一个函数，用来下载网页，返回网页内容
# 参数 url 代表所要下载的网页网址。
# 整体代码和之前类似
def download_content(url):
    http = urllib3.PoolManager()
    response = http.request("GET", url)
    response_data = response.data
    html_content = response_data.decode()
    return html_content
# 第二个函数，将字符串内容保存到文件中
# 第一个参数为所要保存的文件名，第二个参数为要保存的字符串内容的变量
def save_to_file(filename, content):
    fo = open(filename,"w", encoding="utf-8")
    fo.write(content)
    fo.close()

# 下载报考指南的网页
url = "https://zkaoy.com/sions/exam"
result = download_content(url)
save_to_file("tips1.html", result)

# 解析网页，并提取出文章的链接和标题
# # 输入参数为要分析的 html 文件名，返回值为对应的 BeautifulSoup 对象
def create_doc_from_filename(filename):
    fo = open(filename, "r", encoding='utf-8')
    html_content = fo.read()
    fo.close()
    doc = BeautifulSoup(html_content)
    return doc

doc = create_doc_from_filename("tips1.html")
post_list = doc.find_all("div",class_="post-info")
for post in post_list:
    link = post.find_all("a")[1]
    print(link.text.strip())